package com.orangelife.mobile.individual.activity;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

import com.curry.android.http.HttpUtil;
import com.curry.android.util.DialogHelper;
import com.curry.orangelife.mobile.R;
import com.markupartist.android.widget.PullToRefreshListView;
import com.markupartist.android.widget.PullToRefreshListView.IXListViewListener;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.util.JsonReponseHandler;
import com.thirdlib.test.TestAdapter;

public class FreezeExplainActivity extends OrangeLifeBaseActivity {
	/** 一页中数据的条数，初始设置为7条 */
	private int pageSize = Constant.PAGE_SIZE_DEFAULLT;
	/** 初始页，初始化为第一页 */
	private int pageNo = Constant.PAGE_NO_DEFAULT;
	private int flags = 1;
	private TextView tvTitle;
	private PullToRefreshListView lvExplain;
	private TestAdapter<Map<String, Object>> adapter;
	private List<Map<String, Object>> explainList;
	private List<Map<String, Object>> newList;
	private Dialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_freeze_explain);
		initView();
		setAdapter();
		pullListView();
		getDataFromServer(pageSize, pageNo);
	}

	private void pullListView() {
		lvExplain.setXListViewListener(new IXListViewListener() {
			@Override
			public void onRefresh() {
				handler.postDelayed(new Runnable() {
					@Override
					public void run() {
						flags = 1;
						pageNo = 1;
						updateListViewData();
					}
				}, 2000);
			}
			
			@Override
			public void onLoadMore() {
				handler.postDelayed(new Runnable() {
					@Override
					public void run() {
						flags = 0;
						pageNo += 1;
						updateListViewData();
					}
				}, 2000);
				
			}
		});
	}
	
	private void updateListViewData() {
		getDataFromServer(pageSize,pageNo);
		onLoad();
		if(explainList!=null){
			adapter.setList(explainList);
			adapter.notifyDataSetChanged();
		}
	}
	
	private void onLoad() {
		lvExplain.stopRefresh();
		lvExplain.stopLoadMore();
		lvExplain.setRefreshTime(new Date().toLocaleString());
	}
	
	private void setAdapter() {
		adapter = new TestAdapter<Map<String, Object>>(this,
				R.layout.item_lv_freeze_explain, explainList);
		adapter.addField("frozenMoney",R.id.tvMoney);
		adapter.addField("orderID", R.id.tvOrderNum);
		adapter.addField("item", R.id.tvSection);
		lvExplain.setAdapter(adapter);
	}

	private void getDataFromServer(int pageSize, int pageNo) {
		dialog = DialogHelper.getInstance().createLoadingDialog(
				FreezeExplainActivity.this, "加载数据中...");
		dialog.show();
		String url = Constant.URL_FREEZE_EXPLAIN;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pageIndex", pageNo);
		map.put("pageSize", pageSize);
		JSONRequest.getInstance().sendRequest(JSONRequest.GET, url, map,
				handler, 0, HttpUtil.ACTION_URI);
	}

	private void initView() {
		explainList = new ArrayList<Map<String,Object>>();
		lvExplain = (PullToRefreshListView) findViewById(R.id.lv_explain);
		tvTitle = (TextView) findViewById(R.id.tv_title);
		tvTitle.setText(getResources().getString(R.string.freeze_explain));
	}

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 0:
				/** 列表查询 */
				newList = JsonReponseHandler.getListFromJson(
						String.valueOf(msg.obj), adapter, dialog);
				updateListView(newList);
				break;

			default:
				break;
			}
		};
	};
	
	private void updateListView(List<Map<String,Object>> newList){
		if(newList==null){
			lvExplain.setPullLoadEnable(false);
			if(flags == 1){
				explainList.clear();
				adapter.setList(explainList);
				adapter.notifyDataSetChanged();
			}
		}
		if(explainList!=null&&newList!=null){
			if(newList.size()==0){
				adapter.setList(explainList);
				adapter.notifyDataSetChanged();
				lvExplain.setPullLoadEnable(false);
			}else{
				if(flags == 1){
				  explainList.clear();
				}
				lvExplain.setPullLoadEnable(true);
				explainList.addAll(newList);
				adapter.setList(explainList);
				adapter.notifyDataSetChanged();
			}
		}
	}
}
